/**
 * 
 */
package com.letoo.dragon.common.utils;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.letoo.dragon.common.exception.BusinessException;

/**
 * aes加密算法
 * 
 * @author dengjie
 *
 */
public class DES3Utils {

    private static final Logger LOG = LoggerFactory.getLogger(DES3Utils.class);

    private final static String IV = "01234567";
    // 加解密统一使用的编码方式
    private final static String ENCODEING = "utf-8";

    /**
     * 3DES加密
     * 
     * @param plainText
     *            普通文本
     * @return
     * @throws Exception
     */
    public static String encrypt(String content, String secretKey) {
        try {
            Key deskey = null;
            DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
            deskey = keyfactory.generateSecret(spec);

            Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
            byte[] encryptData = cipher.doFinal(content.getBytes(ENCODEING));
            return URLEncoder.encode(Base64.encodeBase64String(encryptData), "utf-8");
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new BusinessException("encrypt error", e);
        }
    }

    /**
     * 3DES解密
     * 
     * @param encryptText
     *            加密文本
     * @return
     * @throws Exception
     */
    public static String decrypt(String content, String secretKey) {
        try {
            Key deskey = null;
            DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
            deskey = keyfactory.generateSecret(spec);
            Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
            byte[] contentBytes = Base64.decodeBase64(URLDecoder.decode(content, "utf-8"));
            byte[] decryptData = cipher.doFinal(contentBytes);

            return new String(decryptData, ENCODEING);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new BusinessException("decrypt error", e);
        }
    }

    public static void main(String[] args) {
        /*
         * String code = encrypt("bailong200", "miaomiaoMya-231"); System.out.println(code); String code1 =
         * encrypt("12345678", "miaomiaoMya-231"); System.out.println(code1); System.out.println(decrypt(code,
         * "miaomiaoMya-231"));
         * 
         * // String token = encrypt("10b019df-519e-3908-bc48-92a08b107686", "miAo858Miao-92RE");
         * System.out.println(token); System.out.println(decrypt(token, "miAo858Miao-92RE")); //SIGN String
         * sign="/uc-api/userdetail/detail"+"10b019df-519e-3908-bc48-92a08b107686"+"mIo98aiqing";
         * System.out.println(MD5Utils.md5AndBase64(sign)); String pic="100"+"231349"+System.currentTimeMillis();
         * System.out.println(pic);
         */
        // ddd
        String testStr = encrypt("123456789", "MMryu23Xan4@lx300#$o25#$");
        System.out.println(testStr);
        System.out.println(decrypt(testStr, "MMryu23Xan4@lx300#$o25#$"));

    }
}
